package com.application.model.utils;

public class HTTPClientLogin {
	
	/** 
     * @param args 
     */ 
//    public static void main(String[] args) { 
//         
//        PostMethod method = new PostMethod("https://www.xing.com/app/user"); 
//        method.addParameter("op", "login"); 
//        method.addParameter("dest", "/app/startpage?op=home"); 
//        method.addParameter("s_dest", ""); 
//        method.addParameter("force_dest","1"); 
//        method.addParameter("login_user_name", "aqui tu username"); 
//        method.addParameter("login_password", "aqui tu password"); 
//        method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); 
//         
//        HttpClient client = new HttpClient(); 
//        String page = getServerPage(client, method); 
//        System.out.println(page); 
//
//    } 
//     
//    private static void sendToPost(String url, Cookie[] cookies) { 
//        PostMethod method = new PostMethod(url); 
//        method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); 
//        HttpClient client = new HttpClient(); 
//        HttpState initialState = new HttpState(); 
//        for(Cookie cookie : cookies) { 
//            initialState.addCookie(cookie);     
//        } 
//        client.setState(initialState); 
//        String page = getServerPage(client, method); 
//        System.out.println(page); 
//    } 
//     
//    private static String getServerPage(HttpClient client, PostMethod method) { 
//
//        BufferedReader br = null; 
//        StringBuilder sb = new StringBuilder(); 
//        try { 
//            int returnCode = client.executeMethod(method); 
//             
//            if(returnCode == HttpStatus.SC_MOVED_TEMPORARILY) { 
//                // Si hay un Redirect entonces, has otro post con la nueva location y los cookies 
//                Header locationHeader = method.getResponseHeader("location"); 
//                if (locationHeader != null) { 
//                    String redirectLocation = locationHeader.getValue(); 
//                    System.out.println(redirectLocation); 
//                    Cookie[] cookies = client.getState().getCookies(); 
//                    sendToPost(redirectLocation, cookies); 
//                } else { 
//                    // The response is invalid and did not provide the new location for 
//                    // the resource.  Report an error or possibly handle the response 
//                    // like a 404 Not Found error. 
//                } 
//                return ""; 
//            } 
//             
//            System.out.println(returnCode); 
//
//            if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { 
//                System.err 
//                        .println("*** The Post method is not implemented by this URI"); 
//                // still consume the response body 
//                method.getResponseBodyAsString(); 
//            } else { 
//                br = new BufferedReader(new InputStreamReader(method 
//                        .getResponseBodyAsStream())); 
//                String readLine; 
//                while (((readLine = br.readLine()) != null)) { 
//                    System.err.println(readLine); 
//                    sb.append(readLine); 
//                } 
//            } 
//        } catch (Exception e) { 
//            e.printStackTrace(); 
//        } finally { 
//            method.releaseConnection(); 
//            if (br != null) 
//                try { 
//                    br.close(); 
//                } catch (Exception fe) { 
//                } 
//        } 
//        return sb.toString(); 
//    }
}
